<template>
  <div id="kfjh-list">
    <div>
      <el-select
        style="width: 40vw"
        v-model="selectValue"
        placeholder="请选择计划"
        @change="onSelectChange"
        :disabled="selectDisable"
      >
        <el-option
          v-for="item in selectOptions"
          :key="item.scId"
          :label="item.scCustomerName"
          :value="item.scId"
        >
        </el-option>
      </el-select>
    </div>
    <br />
    <el-table
      v-loading="loading"
      :data="tableData"
      border
      style="width: 100%"
      highlight-current-row
      height="60vh"
    >
      <el-table-column
        :resizable="false"
        prop="dpContent"
        label="开发计划"
        width="300"
        fixed
      >
      </el-table-column>
      <el-table-column :resizable="false" prop="dpId" label="编号" width="50">
      </el-table-column>
      <el-table-column
        :resizable="false"
        prop="dpScId"
        label="机会来源编号"
        width="50"
        v-if="false"
      >
      </el-table-column>
      <el-table-column
        :resizable="false"
        prop="dpDate"
        label="创建计划日期"
        width="120"
      >
      </el-table-column>
      <el-table-column
        :resizable="false"
        prop="dpExecutionDate"
        label="执行计划日期"
        width="120"
      >
      </el-table-column>
      <el-table-column
        :resizable="false"
        prop="dpExecutionResults"
        label="执行结果"
        width="300"
      >
      </el-table-column>
      <el-table-column :resizable="false" prop="dpExecutorId" v-if="false">
      </el-table-column>
      <el-table-column
        :resizable="false"
        prop="dpExecutor"
        label="执行人"
        width="100"
      >
      </el-table-column>

      <el-table-column
        :resizable="false"
        fixed="right"
        label="操作"
        width="120"
      >
        <template slot-scope="scope">
          <!-- v-if="scope.row.oppStateId == 1001" -->
          <el-button
            size="mini"
            type="success"
            :disabled="
              scope.row.dpExecutionResults != '' &&
              scope.row.dpExecutionResults != null
            "
            @click="handleEdit(scope.$index, scope.row)"
            >完成执行</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="执行回执" :visible.sync="dialogFormVisible">
      <el-form
        label-position="right"
        label-width="110px"
        ref="formLabelAlign"
        :model="formLabelAlign"
        :rules="rules"
        ><!-- :inline="true":自动多行处理 -->
        <el-form-item prop="dpId" v-if="false">
          <el-input disabled v-model="formLabelAlign.dpId"></el-input>
        </el-form-item>
        <el-form-item
          label="执行结果"
          prop="dpExecutionResults"
          :rules="{ required: true, message: '请输入执行结果!' }"
        >
          <el-input
            type="textarea"
            placeholder="请输入执行结果"
            v-model="formLabelAlign.dpExecutionResults"
          ></el-input>
        </el-form-item>

        <div style="padding-top: 10px"></div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('formLabelAlign')">
          确 认
        </el-button>
        <el-button @click="dialogFormVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectValue: null,
      selectOptions: null,
      loading: false,
      selectDisable: true,
      tableData: null,
      dialogFormVisible: false,
      formLabelAlign: {
        dpId: null,
        dpContent: "", //开发计划
        dpScId: "", //机会来源编号
        dpDate: "", //创建计划日期
        dpExecutionDate: "", //执行计划日期
        dpExecutionResults: "", //执行结果
        dpExecutor: "", //执行人
        dpExecutorId: "", //执行人id
      },
      rules: {
        dpContent: [
          { required: true, message: "请输入开发计划!", trigger: "change" },
        ],
        oppId: [
          { required: true, message: "请选择机会来源!", trigger: "change" },
        ],
      },
    };
  },
  created: function () {
    // /dev-plan/chance
    this.$axios({
      method: "get",
      crossdomain: true,
      url: "/dev-plan/chance",
    }).then((response) => {
      // console.log(response.data.data[0]); //请求成功返回的数据
      this.selectOptions = response.data.data[0];
      //   console.log(this.tableData);
      this.selectDisable = false;
    });
  },
  methods: {
    onSelectChange(opt) {
      // console.log("select", opt);
      this.loading = true;
      this.selectDisable = true;
      this.$axios({
        method: "get",
        crossdomain: true,
        url: "/dev-plan/plan/" + opt,
      }).then((response) => {
        //   console.log(response.data.data); //请求成功返回的数据 列表信息
        this.tableData = response.data.data[0];
        this.tableData.forEach((e) => {
          e.dpExecutor = this.$store.state.userData.name;
          e.dpExecutorId = this.$store.state.userData.id;
          e.dpDate = e.dpDate.substr(0, 10);
          if (e.dpExecutionDate) {
            e.dpExecutionDate = e.dpExecutionDate.substr(0, 10);
          }
        });
        //   console.log(this.tableData);
        this.loading = false;
        this.selectDisable = false;
      });
    },
    handleEdit(index, row) {
      this.formLabelAlign = JSON.parse(JSON.stringify(row));
      this.formLabelAlign.row = row; // 添加原型数据方便修改
      // console.log(this.formLabelAlign); //查看转换后
      this.dialogFormVisible = true;
      // console.log("edit", index, row);
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        // console.log("submit:",this.formLabelAlign);
        if (valid) {
          let params = {
            //给后台发送数据
            dpId: this.formLabelAlign.dpId, //id
            dpScId: this.formLabelAlign.dpScId,
            dpExecutionResults: this.formLabelAlign.dpExecutionResults, //执行结果
          };
          this.$axios({
            method: "put",
            crossdomain: true,
            url: "/dev-plan/results",
            params: params,
          }).then((response) => {
            // console.log(response); //请求成功返回的数据
            if (response.data.type == "OK") {
              // this.formLabelAlign.row = Object.assign({}, this.formLabelAlign.row, params);
              // console.log(this.formLabelAlign.row);
              this.$notify({
                title: "成功啦",
                type: "success",
                duration: "1000", // 1000ms后关闭
                showClose: false,
              });
              // this.$router.replace({ path: "/" });
              this.dialogFormVisible = false;
              this.onSelectChange(this.formLabelAlign.dpScId); //刷新列表
            } else {
              this.$notify({
                title: "失败惹",
                message: response.data.msg,
                type: "error",
                duration: "4000", // 1000ms后关闭
                showClose: false,
              });
            }
          });
        } else {
          this.$notify({
            title: "错了哦",
            message: "还有选项没有填对呢",
            type: "error",
            duration: "4000", // 1000ms后关闭
            showClose: false,
          });
          return false;
        }
      });
    },
  },
};
</script>
